<?php
require_once "./wp-config.php";
define("TABLE","txn_signup");
header("Content-Type: text/html; charset=utf-8");
if(!isset($_GET['sec_wx_id']))
	die("unknown_coming");

$db = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
if($db->connect_errno)
	die("fail connect to database");
$sql = "Select `wx_id` From `".TABLE."` Where `wx_id` = '".$_GET['sec_wx_id']."' LIMIT 1";
$res = $db->query($sql);
?>
<!DOCTYPE html>
<html>
	<head>
		<title>TEDxNUAA2014大会注册</title>
	</head>
	<body>
<?php if(isset($_GET['action'])&&$_GET['action']=='delete')
{
	$sql = "Delete From `".TABLE."` Where `wx_id` = '".$_GET['sec_wx_id']."'";
	$db->query($sql);
?>
		<p>已取消注册</p>
<?php }
elseif($res->num_rows>0){?>
		<p>您已注册</p>
		<a href="./wx_signup.php?sec_wx_id=<?php echo $_GET['sec_wx_id'] ;?>&action=delete">取消注册</a>
<?php
}
elseif(!isset($_POST['name'])){?>
		<form action="wx_signup.php?sec_wx_id=<?php echo $_GET['sec_wx_id'] ;?>" method="post">
		<table>
			<tr>
				<td>姓名</td>
				<td><input type="text" name="name"/></td>
			</tr>
			<tr>
				<td>学号</td>
				<td><input type="number" name="school_id"/></td>
			</tr>
			<tr>
				<td>电话</td>
				<td><input type="number" name="phone"/></td>
			</tr>
			<tr>
				<td>其他</td>
				<td>
					<textarea rows="3" cols="20" name="comment"></textarea>
				</td>
			</tr>
			<tr>
				<td colspan="2"><input type="submit"/></td>
			</tr>
		</table>
		</form>
<?php
	}
	else
	{
		$sql = "Insert Into `".TABLE."` (`school_id`,`name`,`phone`,`comment`,`wx_id`)VALUES('".$_POST['school_id']."','".$_POST['name']."','".$_POST['phone']."','".$_POST['comment']."','".$_GET['sec_wx_id']."')";
		$db->query($sql);
?>
	<p>注册成功！您是第<?php echo $db->insert_id;?>位注册的小伙伴！</p>
<?php
	}
?>
	</body>
</html>